package com.base.cn.platform.os.common.utils.pay;

import com.base.cn.platform.os.common.md5.MD5;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.http.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

/**
 * 支付接口
 *
 * @author s.li
 * @create 2018-09-12-10:01
 */
@Slf4j
public abstract class PayService {
    public Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    /**
     * 获取支付跳转路径或支付数据方法
     * @param request  HttpServletRequest
     * @param payConfigMap 支付类型的配置数据
     * @param params 订单、回调路径、用户信息等数据参数
     * @param clientType 客户端类型，分PC、H5、APP、小程序
     * @return 跳转支付的凭证数据Map<String,Object>
     */
    public abstract Map<String,Object> goToPay(HttpServletRequest request,
                                               Map<String,String> payConfigMap,
                                               Map<String,String> params,
                                               String clientType) throws Exception ;

    /**
     * 支付回调
     * @param request HttpServletRequest
     * @param payConfigMap 支付类型的配置数据
     * @param clientType  客户端类型，分PC、H5、APP、小程序
     * @return Map<String,String>
     */
    public abstract Map<String,String> payCallback(HttpServletRequest request,
                                                   HttpServletResponse response,
                                                   Map<String,String> payConfigMap,
                                                   String clientType) throws Exception;

    /**
     * 转账接口
     * @param request HttpServletRequest
     * @param payConfigMap 支付自信Map
     * @param params 自定义参数
     * @return Map<String,Object>
     */
    public abstract Map<String,Object> payTransfer(HttpServletRequest request,
                                                   TreeMap<String,String> payConfigMap,
                                                   Map<String,String> params) throws Exception;

    /**
     * 获取第次支付时生成的订单号
     * @param params 订单、回调路径、用户信息等数据参数
     * @return 随机订单号
     */
    public String getOutTradeNo(Map<String,String> params){
        String outTradeNo = MD5.getMD5(params.get("orderNo")+params.get("userId")+ StringUtils.getRandomString(32)+System.currentTimeMillis()).toUpperCase();
        return outTradeNo;
    }

    /**
     * 获取微信的openId
     * @param payConfigMap 支付配置内容
     * @param appId 微信的APPID
     * @param appSecret 微信密钥
     * @param payFormClient 支付终端
     * @param code 微信code
     * @return 返回openId
     */
    public String weiXinOpenId(Map<String,String> payConfigMap,String appId,String appSecret,String payFormClient,String code)throws IOException {
        String result = null;
        if(PayFormClient.H5.equals(payFormClient)){
            result = HttpUtil.doGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appId+"&secret="+appSecret+"&code="+code+"&grant_type=authorization_code");
        }else if(PayFormClient.APPLY.equals(payFormClient)){
            result = HttpUtil.doGet("https://api.weixin.qq.com/sns/jscode2session?appid="+appId+"&secret="+appSecret+"&js_code="+code+"&grant_type=authorization_code");
        }
        Map<String,String> resultMap = gson.fromJson(result,new TypeToken<Map<String,String>>(){}.getType());
        log.info("------------------weixin_user_info:"+resultMap.toString());
        String openid = resultMap.get("openid");
        log.info("------------------openid:"+openid);
        payConfigMap.put("openid",openid);
        return openid;
    }
}
